package com.wangyadong.hobby.schedule.jobtask.ath.array;

import java.math.BigDecimal;
import java.util.Random;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2018/12/1110:27
 **/
public class Example {

    public static void main(String[] args) {
        Integer[] integers = new Integer[20];
        for (int i = 0; i < 20; i++) {
            integers[i] = new Random().nextInt(100);
            System.out.println(integers[i]);
        }
        System.out.println("===================================");
//        insertionSort(integers);
        shellSort(integers);
        for (int i = 0; i < 20; i++) {
            System.out.println(integers[i]);
        }
        System.out.println("===================================");
        int i = 10;
        int h = 5;
        System.out.println(h -= h);
    }

    public static void insertionSort(Comparable[] a) {
        int length = a.length;
        for (int i = 1; i < length; i++) {
            for (int j = i; j > 0 && (a[j].compareTo(a[j - 1]) < 0); j--) {
//                exch(a, j, j - 1);
                Comparable t = a[j];
                a[j] = a[j - 1];
                a[j - 1] = t;
            }
        }
    }

    public static void shellSort(Comparable[] a) {
        int length = a.length;
        int h = 1;
        while (h < length / 3) {
            h = h * 3 + 1;
        }
        while (h >= 1) {
            for (int i = h; i < length; i++) {
                for (int j = i; j >= h && (a[j].compareTo(a[j - h]) < 0); j -= h) {
                    Comparable t = a[j];
                    a[j] = a[j - h];
                    a[j - h] = t;
                }
            }
            h = h / 3;
        }


    }

    private static boolean less(Comparable v, Comparable w) {
        return v.compareTo(w) < 0;
    }

    private static void exch(Comparable[] a, int i, int j) {
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

    private static void show(Comparable[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i] + "");
        }
        System.out.println("");
    }

    public static boolean isSort(Comparable[] a) {
        for (int i = 0; i < a.length; i++) {
            if (less(a[i], a[i - 1])) {
                return false;
            }
        }
        return true;
    }

}
